package com.jy.rhin.infra.repo.document.content.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.rhin.infra.repo.document.content.mapper.DocumentContentMapper;
import com.jy.rhin.infra.repo.document.content.po.DocumentContentPO;
import com.jy.rhin.infra.repo.document.submission.mapper.DocumentEntryMapper;
import com.jy.rhin.infra.repo.document.submission.po.DocumentEntryPO;
import com.jy.rhin.sdk.dto.document.DocumentContentDto;
import com.jy.rhin.sdk.dto.document.query.GetContentDto;
import com.jy.rhin.sdk.dto.document.query.GetContentReqDto;
import com.jy.rhin.sdk.dto.document.query.GetContentRespDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author dhl
 * @date 2022/6/1
 * @Description
 */
@Service
public class DocumentContentQueryHandler {

    @Resource
    DocumentContentMapper documentContentMapper;

    @Resource
    DocumentEntryMapper documentEntryMapper;


    public DocumentContentDto getContent(GetContentDto dto) {
        QueryWrapper<DocumentEntryPO> entryQueryWrapper = new QueryWrapper<>();
        entryQueryWrapper.lambda().eq(StringUtils.isNotEmpty(dto.getUniqueId()), DocumentEntryPO::getUniqueId, dto.getUniqueId())
                .eq(StringUtils.isNotEmpty(dto.getRepositoryUniqueId()), DocumentEntryPO::getRepositoryUniqueId, dto.getRepositoryUniqueId());
        DocumentEntryPO entryPo = documentEntryMapper.selectOne(entryQueryWrapper);
        if (Objects.isNull(entryPo)) {
            return null;
        }
        QueryWrapper<DocumentContentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(dto.getUniqueId()), DocumentContentPO::getUniqueId, dto.getUniqueId())
                .eq(StringUtils.isNotEmpty(dto.getRepositoryUniqueId()), DocumentContentPO::getRepositoryUniqueId, dto.getRepositoryUniqueId());
        DocumentContentPO contentPo = documentContentMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(contentPo)) {
            return null;
        }
        DocumentContentDto result = new DocumentContentDto();
        BeanUtils.copyProperties(contentPo, result);
        result.setTypeCode(entryPo.getTypeCode());
        result.setTitle(entryPo.getTitle());
        result.setHuanZheXm(entryPo.getHuanZheXm());
        return result;
    }

    /**
     * 获取文档内容列表
     *
     * @param req
     * @return
     */
    public List<GetContentRespDto> getDocumentContentList(GetContentReqDto req) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        List<GetContentDto> dtoList = req.getDtoList();
        for (GetContentDto dto : dtoList) {
            queryWrapper.or(wrapper -> wrapper.and(wp -> wp.eq("a.unique_id", dto.getUniqueId()).eq("a.repository_unique_id", dto.getRepositoryUniqueId())));
        }
        List<GetContentRespDto> contentRespList = documentContentMapper.queryDocumentContentList(queryWrapper);
        if (!CollectionUtils.isEmpty(contentRespList)) {
            return contentRespList;
        }
        return Collections.emptyList();
    }
}
